﻿$(document).ready(function() {
    var today = new Date();

    $(".from-date.datepicker").datepicker({
        changeMonth: true,
        numberOfMonths: 1,
        showOn: "button",
        maxDate: today,
        onClose: function(selectedDate) {
            $(".to-date.datepicker").datepicker("option", "minDate", selectedDate);
        }
    });
    $(".to-date.datepicker").datepicker({
        changeMonth: true,
        numberOfMonths: 1,
        showOn: "button",
        maxDate: today,
        onClose: function(selectedDate) {
            $(".from-date.datepicker").datepicker("option", "maxDate", selectedDate);
        }
    });

    $('#centeredmenu li a').click(function () {
        if ($(this).closest('li').hasClass('current')) return;
        status = $(this).attr('class');
        loadData(1);

        $('#centeredmenu li.current').removeClass('current');
        $(this).closest('li').addClass('current');
    });
    
    $('#button-find').click(function() {
        loadData(1);
    });

    loadData(1);
    
});

var status = 'pending';
var currentPage, pageNum;

function loadData(pageNum, isPaged) {
    var result = requestFormImplement.getList(pageNum, $('#keyword').val());

    if (result != null) {
        $('#list').setTemplateURL("/templates/admin-list.htm");
        $('#list').processTemplate(result);

        if (!isPaged) {
            paginator(result.RowCount, result.PageSize);
        }
    }

    currentPage = pageNum;
}

function paginator(rowCount, pageSize) {
    $('#pager').smartpaginator({
        totalrecords: rowCount,
        recordsperpage: pageSize,
        length: 5,
        next: 'Next',
        prev: 'Prev',
        first: 'First',
        last: 'Last',
        theme: 'blue',
        controlsalways: true,
        onchange: function(newPage) {
            loadData(newPage, true);
            currentPage = newPage;
            $('#r').html('Page # ' + newPage);
        }
    });
}

var requestFormImplement = new Object(
    {
        getList: function(pageNum, keyword) {
            var list;
            $.ajax({
                type: "POST",
                url: "/Pages/PendingConfirmList.aspx/GetList",
                data: JSON.stringify({ status: status, pageNum: pageNum, keyword: keyword }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function(data) {
                    list = data.d;
                },
                error: function() {
                    list = null;
                }
            });

            return list;
        }
    });